function X = MergePatches(MatPath, MatNameFormat, x)
% MatNameFormat = 'tempdata*.mat';

if nargin < 3, x = []; end

len = length(dir(fullfile(MatPath, MatNameFormat)));
MatNameFormat = strrep(MatNameFormat, '*', '%d');

disp('Merging all image patches to X and Y, respectively ...');
X = cell(1, len);
for i = 1 : len
    fprintf('\tMerge %d / %d\n', i, len);
    X{i} = importdata(fullfile(MatPath, sprintf(MatNameFormat, i)));
    if isstruct(X{i})
        X{i} = X{i}.xx;
    end
end
X = cat(3, X{:}, x);
disp('Marging done!')

%%  for BN
% X = X * 255;
% Y = Y * 255;
